{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "67e4d8f9",
   "metadata": {},
   "source": [
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week07"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a7da944",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "* 认知数据结构（4种）\n",
    "* 列表\n",
    "* 元组\n",
    "* 集合\n",
    "* 字典\n",
    "\"一切皆是对象\"\n",
    "* 列表学习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3da69bad",
   "metadata": {},
   "source": [
    "* 思考：如果把我们班的同学名字都存起来，你会怎么做，用变量name1，name...来存放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ec364609",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"李华\"\n",
    "name2 = \"小鹏\"\n",
    "name3 = \"张三\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e6383d78",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4117b658",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小鹏'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a40d477b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3bb3b187",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"李华，小鹏，张三\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "98667ff4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华，小鹏，张三'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3abd73cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小鹏'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[3:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f19d3bfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"小鹏\",\"李华\",\"李小明\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a3545673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '小鹏', '李华', '李小明']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9a259bd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "92360826",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小鹏'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0cc53b49",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_学号 = [2210130072,2210130071,2210130081,2210130085]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f6302c68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2210130072, 2210130071, 2210130081, 2210130085]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b254ac2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树仁\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ff06a1ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树仁', '李华', '李小明']"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b5dad689",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_张三 = [\"张三\",2210130072,19,18,180,75,[\"python\",\"移动网络\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cc815d9",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bd25c587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 2210130072, 19, 18, 180, 75, ['python', '移动网络', 'UI设计']]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_张三"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c93ce4c8",
   "metadata": {},
   "source": [
    "* 虽然列表可以存放不同类型的数据，但是一般情况我们存放的数据都是同一种数据类型的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "327b1ce9",
   "metadata": {},
   "source": [
    "# 允许重复的数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bd7a028",
   "metadata": {},
   "source": [
    "# 列表学习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fba0f3ce",
   "metadata": {},
   "source": [
    "* Ordered: Maintain the order of the data insertion.\n",
    "* 保持数据插入的顺序**(有序性)**\n",
    "1.列表中的元素可以被修改。我们可以在创建列表后向列表添加或删除项目。\n",
    "* Changeable: List is mutable and we can modify items.\n",
    "* 列表是可变的，我们可以修改条目**(可变性)**\n",
    "2.列表中的项目是有序的。每个项都有一个惟一的索引值。新项目将被添加到列表的末尾。\n",
    "* Heterogeneous: List can contain data of different types\n",
    "* 列表可以包含不同类型的数据**(异构性)**\n",
    "3.列表可以包含不同类型的元素，例如;它们可以包含字符串、整数、布尔或任何类型的元素。\n",
    "* Contains duplicate: Allows duplicates data\n",
    "* 允许重复的数据**(数据重复性)**\n",
    "4.列表可以包含重复项，即列表可以有两个具有相同值的项。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0248c3a2",
   "metadata": {},
   "source": [
    "# 创建列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ccc22f7",
   "metadata": {},
   "source": [
    "# Using list constructor 使用构造器创建列表\n",
    "my_list1 = list((1, 2, 3))\n",
    "print(my_list1)\n",
    "# Output [1, 2, 3]\n",
    "\n",
    "# Using square brackets[] 直接使用[]创建列表\n",
    "my_list2 = [1, 2, 3]\n",
    "print(my_list2)\n",
    "# Output [1, 2, 3]\n",
    "\n",
    "# with heterogeneous items 创建不同类型数据结构的列表\n",
    "my_list3 = [1.0, 'Jessa', 3]\n",
    "print(my_list3)\n",
    "# Output [1.0, 'Jessa', 3]\n",
    "\n",
    "# empty list using list() 创建控列表\n",
    "my_list4 = list()\n",
    "print(my_list4)\n",
    "# Output []\n",
    "\n",
    "# empty list using [] 创建控列表\n",
    "my_list5 = []\n",
    "print(my_list4)\n",
    "# Output []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "18ca9430",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 增加\n",
    "help(name_list.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a468fef7",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树仁\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "c30103d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树仁', '李华', '李小明', '李树仁']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "58f8cdc0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "9f6ff29c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"marry\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "98a93767",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树仁', 'marry', '李华', '李小明', '李树仁']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d6d076",
   "metadata": {},
   "source": [
    "# 2.删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "fb47aafb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "a3f6b82d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树仁'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5a915898",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
